function PostProcess(fin, fout)

load(fin);

[vareas, veas, vebs, vephis, vex0s, vey0s, vx1s, vx2s, vy1s, vy2s, vxheights, vyheights, objs] = ShiftIndexes(vareas, veas, vebs, vephis, vex0s, vey0s, vx1s, vx2s, vy1s, vy2s, vxheights, vyheights, objs, nfindobjs);

len = length(objs);

prevxs = zeros(nfindobjs,1);
prevys = zeros(nfindobjs,1);

dxs = zeros(nfindobjs,1);
dys = zeros(nfindobjs,1);

prevlens = zeros(nfindobjs, 1);
lastframeupdates = zeros(nfindobjs, 1);

nparseobjs = zeros(len,1);
bparseobjs = zeros(len,nfindobjs);

bremoves = zeros(len, nfindobjs);
bremoved = zeros(len, 1);

a = find(objs == nfindobjs);
meana = mean(vebs(a,1));
meanb = mean(vebs(a,1));

cdist = 3.0;
cdistmin = 1.25*cdist; 
d = sqrt(meana^2+meanb^2)*cdistmin;


nlastremoved = 0;
binit = 0;

noffbuffer = d*0.25;
maxframes = 5;
maxremove = maxframes;
minmaxdist = d;


%Remove incorrect objs
i = 1;
while i < len
    
    nobjs = objs(i);
   
        
    if binit == 1
     
        exs = zeros(nfindobjs, 1);
        eys = zeros(nfindobjs, 1);
        
        ncount = 0;
        nlastframeobjs = 0;
        
                

        for j = 1:nfindobjs                            
            
            if prevxs(j) > 0 && prevys(j) > 0                
                nstart = lastframeupdates(j);
                nframes = i-nstart;
                
                if nframes == 1
                    nlastframeobjs = nlastframeobjs + 1;
                end
                
                ncount = ncount + 1;
                exs(ncount) = prevxs(j)+dxs(j)*(nframes+1);	
                eys(ncount) = prevys(j)+dys(j)*(nframes+1);     
            else
                dxs(j) = 0;
                dys(j) = 0;
            end
        end
        
        
                                   
        nprevobjs = ncount;
               
        maxlast = max(i-lastframeupdates);
                
        
        if nprevobjs == nfindobjs && objs(i) == nfindobjs && maxlast == 1
            [adists, assignments, bassigned] = AssignUniqueObjDists(nfindobjs, nfindobjs, prevxs, prevys, vex0s(i,:), vey0s(i,:));            
        else
            [adists, assignments] = AssignObjDists(nprevobjs, objs(i), exs, eys, vex0s(i,:), vey0s(i,:));
        end        	    
                
        newxs = prevxs;
        newys = prevys;
        newdxs = zeros(nfindobjs, 1);
        newdys = zeros(nfindobjs, 1);
        newprevlens = prevlens; 
        lastframeupdatesorg = lastframeupdates;        
        
        xs = vex0s(i, :);
        ys = vey0s(i, :);
        
        nremoved = 0;
        
        
        %assign each current object to obj(s) in last frame
        for j = 1:nobjs              
                      
            jprev = assignments(j); %object j is assigned to jprev in previous frame
                          
            if prevxs(jprev) > 0 && prevys(jprev) > 0
                nstart = lastframeupdates(jprev);
                nframes = i-nstart;                                
                
                %prevxs contains position of jprev in previous frame, dxs
                %contains its speed. calculate what the expected position
                %of the object in the current frame is by using xs and dxs
                ex = prevxs(jprev)+dxs(jprev)*nframes;	
                ey = prevys(jprev)+dys(jprev)*nframes;
                              
                x = xs(j);
                y = ys(j);
                
                dist = sqrt((x-ex)^2+(y-ey)^2); %distance between expected position and object position            
                         
                a = veas(i, j);
                b = vebs(i, j);
                                
                maxdist = max([sqrt(a^2+b^2)*cdist, prevlens(jprev)*cdist]);           
    
                if maxdist < minmaxdist
                    maxdist = minmaxdist;
                end                
               
                      
                if dist > maxdist && nframes < maxframes && binit == 1 && nbremove < maxremove %&& nmisobjframes < nmaxmisobjframes %is its current position further away than expected? (ex/ey contains expected position)                                                                         
                    bremoves(i, j) = 1;
                    bremoved(i) = 1;          
                    
                    nremoved = nremoved + 1;
                else                                                                
                    if nbremove >= maxremove 
                        bremoves(nlastremoved:i, :) = 0;
                        nparseobjs(nlastremoved:i) = 0;
                        bparseobjs(nlastremoved:i,:) = 0;
                        binit = 0;
                        
                        nremoved = 0;
                    end                    
                end                                                   
            else
                binit = 0;
            end                                                                                   
        end    
        
        
        if nobjs >= nprevobjs && nobjs > 0 && bremoved(i) == 0
            [adists, assignments] = AssignObjDists(nprevobjs, objs(i), exs, eys, xs, ys);
                
            newxs = zeros(nfindobjs, 1);
            newys = zeros(nfindobjs, 1);
            newdxs = zeros(nfindobjs, 1);
            newdys = zeros(nfindobjs, 1);
            lastframeupdates = zeros(nfindobjs, 1);            
            
            for j = 1:nobjs            
                                       
                jprev = assignments(j); %object j is assigned to jprev in previous frame
                          
                if prevxs(jprev) > 0 && prevys(jprev) > 0                       
                    %prevxs contains position of jprev in previous frame, dxs
                    %contains its speed. calculate what the expected position
                    %of the object in the current frame is by using xs and dxs
                    ex = exs(jprev);
                    ey = eys(jprev);
                              
                    x = xs(j);
                    y = ys(j);
                                              
                    dist = sqrt((x-ex)^2+(y-ey)^2); %distance between expected position and object position            
                         
                    a = veas(i, j);
                    b = vebs(i, j);
                                
                    maxdist = max([sqrt(a^2+b^2)*cdist, prevlens(jprev)*cdist], minmaxdist);                                                               
                   
                                                                                    
                      
                    if dist <= maxdist
                        newxs(j) = x;
                        newys(j) = y;
                    
                        a = veas(i, j);
                        b = vebs(i, j);
            
                        newprevlens(j) = sqrt(a^2+b^2);                        
                    
                        lastframeupdates(j) = i;                          
                     end                    
                end                        
            end
        elseif nobjs < nprevobjs && nobjs > 0 && bremoved(i) == 0
            [adists, assignments] = AssignObjDists(objs(i), nprevobjs, xs, ys, exs, eys);
             
             %boffscreen = IsOffScreen(exs, eys, imgx, imgy, nprevobjs, noffbuffer);                                      
                
           
             %if boffscreen == 0
                 
                 nparseobj = 0;
                 
                 for j = 1:nprevobjs                     
                        jcurrent = assignments(j); %object jcurrent is assigned to j in previous frame
                        
                        ex = exs(j);
                        ey = eys(j);
                        
                        x = xs(jcurrent);
                        y = ys(jcurrent);                                                
                            
                        dist = sqrt((x-ex)^2+(y-ey)^2);
                        
                        a = veas(i, jcurrent);
                        b = vebs(i, jcurrent);
                                
                        maxdist = max([sqrt(a^2+b^2)*cdist, prevlens(jcurrent)*cdist, minmaxdist]); 
                            
                      
             
                        if dist > maxdist
                            nparseobj = nparseobj + 1;
                        else                            
                            newxs(j) = x;
                            newys(j) = y;
                    
                            a = veas(i, j);
                            b = vebs(i, j);
            
                            newprevlens(j) = sqrt(a^2+b^2);                        
                    
                            lastframeupdates(j) = i;  
                        end        
                 end
                 
                 
                 if nparseobj > 0                     
                     ncount = 0;
                     
                     for j = 1:nfindobjs                          
                         if xs(j) == 0 && ncount < nparseobj
                             bparseobjs(i,j) = 1;
                             
                             nparseobjs(i) = nparseobjs(i) + 1;
                             
                             ncount = ncount + 1;
                         end
                     end
                 end
                 
             %end
        end
        
        
        if bremoved(i) == 1
            if nbremove == 0
                nlastremoved = i;
            end
            nbremove = nbremove + 1;
        else
            nbremove = 0;
        end
            
              
        prevxs = newxs;
        prevys = newys;
        dxs = newdxs;
        dys = newdys;
        prevlens = newprevlens;
        
      
        
        a = find(prevxs > 0);
        
        if isempty(a)
            binit = 0;
        end               
        
        
    elseif binit == 0 && objs(i) == nfindobjs && objs(i+1) == nfindobjs 
        
        
        [adists, assignments] = AssignObjDists(nfindobjs, nfindobjs, vex0s(i,:), vey0s(i,:), vex0s(i+1,:), vey0s(i+1,:));        	    
                                   
        a = find(assignments == 0);
        
        if isempty(a)
            for j = 1:nfindobjs                
                jprev = assignments(j); %object j is assigned to jprev in previous frame
                                        
                prevxs(j) = vex0s(i+1,j);
                prevys(j) = vey0s(i+1,j);   
            
                dxs(j) = vex0s(i+1,j)-vex0s(i,jprev);
                dys(j) = vey0s(i+1,j)-vey0s(i,jprev);   
        
                a = veas(i+1, j);
                b = vebs(i+1, j);
            
                prevlens(j) = sqrt(a^2+b^2);    
            
                lastframeupdates(j) = i+1;
            end
                
            nbremove = 0;
            binit = 1;        
        
            i = i + 1;
        end        
    end %binit == 1       
         
    i = i + 1;
end


for i = 1:len
	for j = 1:nfindobjs
		if bremoves(i, j) == 1
           
			vareas(i,j) = 0;
            veas(i,j) = 0;
            vebs(i,j) = 0;
            vephis(i,j) = 0;            
            vey0s(i,j) = 0;
            vex0s(i,j) = 0;
            
            vx1s(i,j) = 0;
            vx2s(i,j) = 0;
            vy1s(i,j) = 0;
            vy2s(i,j) = 0;
            
            vxheights(i,j) = 0;
            vyheights(i,j) = 0;      
            
            objs(i) = objs(i) - 1;   
            
         end
    end
    
    if objs(i) == nfindobjs
        nparseobjs(i) = 0;
    end
end




[vareas, veas, vebs, vephis, vex0s, vey0s, vx1s, vx2s, vy1s, vy2s, vxheights, vyheights, objs] = ShiftIndexes(vareas, veas, vebs, vephis, vex0s, vey0s, vx1s, vx2s, vy1s, vy2s, vxheights, vyheights, objs, nfindobjs);


%Extrapolate objs
[objs, vex0s, vey0s, vareas, veas, vebs, vephis, vx1s, vx2s, vy1s, vy2s] = ExtrapolateObjs(objs, vex0s, vey0s, vareas, veas, vebs, vephis, vx1s, vx2s, vy1s, vy2s, nparseobjs, bparseobjs, len, nfindobjs);


x = find(objs == nfindobjs);
y = find(objs > 0);


save(fout, 'objs', 'vx1s', 'vx2s', 'vy1s', 'vy2s', 'vex0s', 'vey0s', 'vephis', 'veas', 'vebs', 'vets', ...
    'vxheights', 'vyheights', 'vareas', 'nprocessstart', 'imgx', 'imgy', 'imghalfx', 'imghalfy', ...    
    'nfindobjs', 'nfindmaxobjs', 'nstartframe', 'nendframe');




    

 




